package com.cn.boot.student.service.daily.impl;

import com.cn.boot.student.entity.DailyEntity;
import com.cn.boot.student.entity.UserEntity;
import com.cn.boot.student.mapper.DailyMapper;
import com.cn.boot.student.service.daily.IDailyService;
import com.cn.boot.student.service.common.impl.RedisServiceImpl;
import com.cn.boot.student.util.CommonResult;
import com.cn.boot.student.util.FreemarkerUtil;
import com.cn.boot.student.util.Tool;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.UUID;

@Service
@CacheConfig(cacheNames = "daily")
public class DailyServiceImpl implements IDailyService {
    @Autowired
    private DailyMapper dailyMapper;
    @Autowired
    private RedisServiceImpl redisService;

    /**
     * 日报列表，分页
     *
     * @param dailyEntity
     * @return
     */
    @Override
    public CommonResult findAll(DailyEntity dailyEntity) {
        //得到日报总数
        Integer count = dailyMapper.getCount(dailyEntity);
        PageHelper.startPage(Tool.toPageNum(dailyEntity.getPageNum()), Tool.toPageSize(dailyEntity.getPageSize()));
        List<DailyEntity> list = dailyMapper.findAll(dailyEntity);
        if (list.size() == 0) {
            return CommonResult.failed("暂无数据请创建！！！");
        }
        PageInfo<DailyEntity> pageInfo = new PageInfo<>(list);
        HashMap<String, Object> map = new HashMap<>();
        map.put("pageInfo", pageInfo);
        map.put("count", count);
        return CommonResult.success(map, "查询成功！！！");
    }

    /**
     * 根据id查询日报
     *
     * @param id
     */
    @Override
    public CommonResult findById(Integer id) {
        DailyEntity dailyEntity = dailyMapper.findById(id);
        return CommonResult.success(dailyEntity);
    }

    /**
     * 添加日报
     *
     * @param dailyEntity
     */
    @Override
    public CommonResult add(DailyEntity dailyEntity) {
        Integer result = dailyMapper.add(dailyEntity);
        if (result == 0) {
            return CommonResult.failed("添加失败！");
        }
        return CommonResult.success(result, "添加成功！");
    }

    /**
     * 更新日报
     *
     * @param dailyEntity
     */
    @Override
    public CommonResult update(DailyEntity dailyEntity) {
        //更新数据时先判断日报是否提交，若已提交则不能修改
        DailyEntity entity = dailyMapper.findById(dailyEntity.getId());
        if (entity.getStatus().equals("已提交")) {
            CommonResult.success("isHave", "日报已提交不能修改");
        }
        Integer result = dailyMapper.update(dailyEntity);
        if (result == 0) {
            return CommonResult.failed("更新失败！！！");
        }
        return CommonResult.success(result, "更新成功！！！");
    }

    /**
     * 删除日报
     *
     * @param id
     */
    @Override
    public CommonResult del(Integer id) {
        Integer result = dailyMapper.del(id);
        if (result == 0) {
            return CommonResult.failed("删除失败！");
        }
        return CommonResult.success(result, "删除了" + result + "条数据");
    }

    /**
     * 批量删除
     *
     * @param ids
     */
    @Override
    public CommonResult dels(List<Integer> ids) {
        ids.stream().forEach(dailyMapper::del);
        return CommonResult.success(ids.size(), "删除了" + ids.size() + "条数据");
    }

    /**
     * 我的日报列表（不需要权限，所有用户都能查询）
     * 查询，分页
     *
     * @param dailyEntity
     * @return
     */
    @Override
    public CommonResult findMyDailies(DailyEntity dailyEntity) {
        Integer count = dailyMapper.getMyDailyCount(dailyEntity);
        PageHelper.startPage(Tool.toPageNum(dailyEntity.getPageNum()), Tool.toPageSize(dailyEntity.getPageSize()));
        //根据用户id查询日报
        List<DailyEntity> list = dailyMapper.findByUserId(dailyEntity.getUserId());
        if (list.size() == 0) {
            return CommonResult.failed("暂无数据,请创建！！！");
        }
        PageInfo<DailyEntity> pageInfo = new PageInfo<>(list);
        HashMap<String, Object> map = new HashMap<>();
        map.put("pageInfo", pageInfo);
        map.put("count", count);
        return CommonResult.success(map, "查询成功！！！");
    }

    /**
     * 生成今日未提交和已提交日报的学员名单，
     * 班主任或者教练可查看今日提交日报名单情况
     * <p>
     * ---得到角色为学员的数据
     */
    @Override
    public CommonResult getStudent(UserEntity userEntity) {
        //得到日报总数
        Integer count = dailyMapper.getStudentCount(userEntity);
        PageHelper.startPage(Tool.toPageNum(userEntity.getPageNum()), Tool.toPageSize(userEntity.getPageSize()));
        List<UserEntity> list = dailyMapper.getStudent(userEntity);

        PageInfo<UserEntity> pageInfo = new PageInfo<>(list);
        HashMap<String, Object> map = new HashMap<>();
        map.put("pageInfo", pageInfo);
        map.put("count", count);
        return CommonResult.success(map, "查询成功！！！");
    }

    /**
     * 下载日报生成word文档
     *
     * @param dailyEntity
     */
    @Override
    public CommonResult download(DailyEntity dailyEntity) {
        //数据赋值
        HashMap<String, Object> map = new HashMap<>();
        map.put("author", dailyEntity.getAuthor());
        map.put("time", dailyEntity.getTime());
        map.put("major", dailyEntity.getMajor());
        map.put("coach", dailyEntity.getCoach());
        map.put("councilor", dailyEntity.getCouncilor());
        map.put("content", dailyEntity.getContent());
        map.put("event", dailyEntity.getEvent());
        map.put("harvest", dailyEntity.getHarvest());
        map.put("starred", dailyEntity.getStarred());
        map.put("doubt", dailyEntity.getDoubt());
        map.put("help", dailyEntity.getHelp());
        //word模板文件
        String templateName = "daily.ftl";
        //文件生成的目标路径
        String filePath = "D:/IdeaProjects/word";
        //文件生成的目标文件名(唯一)
        String fileName =  UUID.randomUUID() + "-" + dailyEntity.getAuthor() + ".doc";
        try {
            FreemarkerUtil.createWord(map, templateName, filePath, fileName);//数据和模板合并生成word
        } catch (Exception e) {
            e.printStackTrace();
            return CommonResult.failed("文件下载失败");
        }
        return CommonResult.success(fileName, "文件下载成功！！！");
    }
}
